import request from '@/utils/request'
import { AxiosPromise } from 'axios'

const getAllResource = (): AxiosPromise<any> => {
  return request({
    method: 'GET',
    url: 'boss/resource/getAll'
  })
}

interface SearchResourceType {
    page?: number
    size?: number
    name?: string
    url?: string
    categoryId?: number | null
}
const searchResource = (data: SearchResourceType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/resource/getResourcePages',
    data
  })
}

const getResourceCategory = (): AxiosPromise<any> => {
  return request({
    method: 'GET',
    url: '/boss/resource/category/getAll'
  })
}

interface CreateOrUpdateResourceType {
    name: string
    categoryId: number | null
    url: string
    description: string
    id?: string | null
}

const createOrUpdateResource = (data: CreateOrUpdateResourceType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/resource/saveOrUpdate',
    data
  })
}

const deleteResource = (id: string): AxiosPromise<any> => {
  return request({
    method: 'DELETE',
    url: `/boss/resource/${id}`
  })
}

interface createOrUpdateCategoryType {
    name: string
    sort: number
    id?: string
}

const createOrUpdateCategory = (data: createOrUpdateCategoryType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/resource/category/saveOrderUpdate',
    data
  })
}

const deleteCategory = (id: string): AxiosPromise<any> => {
  return request({
    method: 'DELETE',
    url: `/boss/resource/category/${id}`
  })
}

interface AllocateRoleResourcesType {
  roleId: number|string
  resourceIdList: number[]
}

const allocateRoleResources = (data: AllocateRoleResourcesType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/resource/allocateRoleResources',
    data
  })
}

const getRoleResources = (roleId: number|string): AxiosPromise<any> => {
  return request({
    method: 'GET',
    url: 'boss/resource/getRoleResources',
    params: {
      roleId
    }
  })
}

export {
  getAllResource,
  searchResource,
  getResourceCategory,
  createOrUpdateResource,
  deleteResource,
  createOrUpdateCategory,
  deleteCategory,
  allocateRoleResources,
  getRoleResources
}
